<?php
// $Id$

/**
 * @file
 * Theme template preprocessor functions.
 */

/**
 * theme_guild_roster($players, $players_per_page);
 */
function template_preprocess_guild_roster(&$variables) {
  $game_module = variable_get('guild_game_module', '');
  $guild_name = variable_get('guild_guild_name', '');
  $game_server = variable_get('guild_game_server', '');
  
  if (!empty($guild_name)) {
    $title = check_plain($guild_name);
    if (!empty($game_server)) {
      $title .= ' @ ' . check_plain($game_server);
    }
    
    // This is a rare occasion where $title has already been run through
    // check_plain() and is therefore passed through drupal_set_title() unchecked.
    drupal_set_title(t('Guild roster - !guild-id', array('!guild-id' => $title)));
  }
  
  $variables['inactive_filter'] = variable_get('guild_inactive_filter', FALSE);
  $variables['filters'] = drupal_get_form('guild_roster_filter_form');
  
  $variables['players'] = theme(
    array('guild_roster_table__' . $game_module, 'guild_roster_table'),
    $variables['players'], $variables['players_per_page']
  );
}

/**
 * theme_guild_roster_table($players, $players_per_page);
 */
function template_preprocess_guild_roster_table(&$variables) {
  $guild_roster_header = _guild_get_roster_header();
  
  $ts = tablesort_init($guild_roster_header);
  $header = '';
  
  foreach ($guild_roster_header as $cell) {
    $cell = tablesort_header($cell, $guild_roster_header, $ts);
    $header .= _theme_table_cell($cell, TRUE);
  }
  
  $variables['header'] = $header;
  $variables['pager'] = theme('pager', NULL, $variables['players_per_page'], 0);
  $variables['inactive_filter'] = variable_get('guild_inactive_filter', FALSE);
  
  if (!empty($variables['players'])) {
    $row = 1;
    
    foreach ($variables['players'] as $nid => $player) {
      $variables['players'][$nid]->name = check_plain($player->name);
      $variables['players'][$nid]->rankName = empty($player->rankName) ? '' : check_plain($player->rankName);
      $variables['players'][$nid]->zebra = ($row % 2 == 0) ? 'even' : 'odd';
      if (user_access('access player content')) {
        $variables['players'][$nid]->link = url('node/' . $nid);
      }
      
      $row++;
    }
  }
  else {
    $variables['players'] = array();
  }
}